﻿using LVYAN.Model;
using LVYAN.Model.AutoMation;
using LVYAN.Utils;
namespace LVYAN.Service.AIAPI
{
    /// <summary>
    /// 
    /// </summary>
    public class ConfigManage : BaseAPI
    {
        /// <summary>
        /// 获取改群的配置
        /// </summary>
        /// <returns></returns>
        public static RoomConfig GetConifg(string RoomName, string RoomType, RobotModel Robot)
        {
            var Cache = new AIConfig().GetConfigModel(nameof(ConfigManage), nameof(GetConifg)).Cache;
            var content = JsonService.ToString(new
            {
                RoomName = RoomName,
                RoomType = RoomType,
                RobotCode = Robot.RobotCode,
                RobotName = Robot.RobotName,
                MacAddress = WinInfo.GetMacAddress(),
                MacIP = WinInfo.GetIP(),
                MacName = WinInfo.GetHostName(),
            });
            var Result = CacheManage<RoomConfig>.GetCache(content, () =>
              {
                  new LoggerService().ResponseMsgDebug("配置", "发送消息-配置", content);
                  var result = new RequestService().PostJson($"{AIAPI}api/aichat/get_robot_configinfo", content);
                  if (string.IsNullOrWhiteSpace(result)) return default;
                  var model = JsonService.ToModel<ReponseData<RoomConfig>>(result);
                  new LoggerService().ResponseMsgDebug("配置", "接受消息-配置", model);
                  return model.Data;
              }, Cache);
            return Result;
        }

        /// <summary>
        /// 是否为机器人
        /// </summary>
        /// <returns></returns>
        public static bool Exists(RobotModel Robot, out RobotRobotOther RobotOther)
        {
            var Cache = new AIConfig().GetConfigModel(nameof(ConfigManage), nameof(Exists)).Cache;
            var content = JsonService.ToString(new
            {
                RobotName = Robot.RobotName,
                RobotCode = Robot.RobotCode,
                RobotAvatar = Robot.RobotAvatar,
                MacAddress = WinInfo.GetMacAddress(),
                MacIP = WinInfo.GetIP(),
                MacName = WinInfo.GetHostName(),
            });
            var Result = CacheManage<ReponseData<RobotRobotOther>>.GetCache(content, () =>
            {
                new LoggerService().ResponseMsgDebug("配置", "发送消息-机器人", content);
                var result = new RequestService().PostJson($"{AIAPI}api/aichat/IsItaRobot", content);
                if (string.IsNullOrWhiteSpace(result)) return new ReponseData<RobotRobotOther> { ActionOK = false, };
                var model = JsonService.ToModel<ReponseData<RobotRobotOther>>(result);
                new LoggerService().ResponseMsgDebug("配置", "接受消息-机器人", model);
                return model;
            }, Cache);
            RobotOther = Result.Data;
            return Result.ActionOK;
        }



        /// <summary>
        /// 获取记忆体ID
        /// </summary>
        /// <returns></returns>
        public static string GetMemoryId(RobotRobotOther Robot, RoomConfigModel RoomConfig, string Key, string Description)
        {
            var Cache = new AIConfig().GetConfigModel(nameof(ConfigManage), nameof(GetMemoryId)).Cache;
            var content = JsonService.ToString(new
            {
                RobotId = Robot.Id,
                WorkspaceId = RoomConfig.WorkspaceId,
                Key = new SignService().SignMD5(Key),
            });
            var Result = CacheManage<ReponseData<string>>.GetCache(content, () =>
            {
                var result = new RequestService().PostJson($"{AIAPI}api/aichat/GetMemoryId", content);
                if (string.IsNullOrWhiteSpace(result)) return new ReponseData<string> { ActionOK = false, Data = "" };
                var model = JsonService.ToModel<ReponseData<string>>(result);
                if (!model.ActionOK)
                {
                    var result1 = new BaiLianService().CreateMemoryId(Robot, RoomConfig, Description);
                    if (result1.Success)
                    {
                        var content1 = JsonService.ToString(new
                        {
                            RobotId = Robot.Id,
                            WorkspaceId = RoomConfig.WorkspaceId,
                            Key = new SignService().SignMD5(Key),
                            Value = Key,
                            MemoryId = result1.ResponseId,
                            RequestId = result1.RequestId,
                            Description,
                        });
                        var result2 = new RequestService().PostJson($"{AIAPI}api/aichat/AddMemoryId", content1);
                        return new ReponseData<string> { ActionOK = true, Data = result1.ResponseId };
                    }
                    return new ReponseData<string> { ActionOK = false, Data = "" };
                }
                return model;
            }, Cache);
            return Result.Data;
        }
    }
}
